package com.kc.web.controller.business;

import com.kc.business.domain.BizBusinessCoupon;
import com.kc.business.service.IBizBusinessCouponSerrvice;
import com.kc.common.annotation.Log;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/business/businessCoupon")
public class BizBusinessCouponController extends BaseController {
    @Resource
    private IBizBusinessCouponSerrvice businessCouponSerrvice;
    @PreAuthorize("@ss.hasPermi('business:businessCoupon:getCouponList')")
    @GetMapping(value = "/getCouponList")
    public TableDataInfo getGoodsList(@Param("businessId") Integer businessId, @Param("status") Integer status){
        startPage();
        List<BizBusinessCoupon> kcBusinessCouponList = businessCouponSerrvice.getCouponList(businessId,status);
        return getDataTable(kcBusinessCouponList);
    }
    @Log(title = "商家优惠券管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('business:businessCoupon:couponAdd')")
    @PostMapping(value = "/couponAdd")
    public AjaxResult couponAdd(@Valid @RequestBody BizBusinessCoupon bizBusinessCoupon){
        if(bizBusinessCoupon.getLimitMoney() == null || bizBusinessCoupon.getLimitMoney() <= 0.0){
            bizBusinessCoupon.setIsLimitMoney(0);
        }else{
            bizBusinessCoupon.setIsLimitMoney(1);
        }
        return toAjax(businessCouponSerrvice.addCoupon(bizBusinessCoupon));
    }
    @Log(title = "商家优惠券管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('business:businessCoupon:couponRemove')")
    @DeleteMapping(value = "/couponRemove")
    public AjaxResult couponRemove(@RequestBody Map<String,Integer> map){
        Integer businessId = map.get("businessId");
        Integer couponId = map.get("couponId");
        if(null == businessId || null == couponId){
            return toAjax(0);
        }
        //查询优惠券详情
        BizBusinessCoupon bizBusinessCoupon = businessCouponSerrvice.getCouponInfo(couponId);
        if(!businessId.equals(bizBusinessCoupon.getBusinessId())){
            return toAjax(0);
        }
        bizBusinessCoupon.setIsDeleted(1);
        businessCouponSerrvice.updateCoupon(bizBusinessCoupon);
        return toAjax(1);
    }
    @GetMapping(value = "/getCouponUserList")
    @PreAuthorize("@ss.hasPermi('business:businessCoupon:getCouponUserList')")
    public TableDataInfo getCouponUserList(Integer couponId){

        startPage();
        return getDataTable(businessCouponSerrvice.getCouponUserList(couponId));
    }

}
